package com.alianzamedica.servlets;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import com.alianzamedica.tools.Enviroment;

/**
 * @author Carlos
 * 
 */
public class InitServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1452699991167661188L;

	@Override
	public void init() throws ServletException {
		super.init();
		String path = this.getServletContext().getRealPath("/");
		System.out.println("path cargado: " + path);
		Enviroment enviroment = Enviroment.getInstance();
		enviroment.setPath(path);
		String documentLocation = path
				+ this.getInitParameter("document-mapping");
		try {
			InputStream in = this.readDocument(documentLocation);
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(in);
			enviroment.setDocument(doc);
			System.out.println("documento cargado");
			Properties props = this.readConnectionProps();
			enviroment.setConnectionProperties(props);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * lee el documento y regresa un inputstream.
	 * 
	 * @param location
	 *            localizacion del xml.
	 * @return inputstream para lectura del documento.
	 */
	public InputStream readDocument(String location) {
		FileInputStream reader = null;
		InputStream in = null;
		try {
			StringBuffer buffer = new StringBuffer();
			reader = new FileInputStream(location);
			int read = 0;
			while ((read = reader.read()) != -1) {
				buffer.append((char) read);
			}
			String xml = buffer.toString();
			String regex = "<!.*>";
			xml = xml.replaceAll(regex, "");
			in = new ByteArrayInputStream(xml.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (Exception e) {
			}
		}
		return in;
	}

	private Properties readConnectionProps() throws Exception {
		String cProp = this.getInitParameter("connection.props");
		String path = this.getServletContext().getRealPath("/");
		String propsPath = path + cProp;
		Properties props = new Properties();
		InputStream in = null;
		try {
			File file = new File(propsPath);
			in = new FileInputStream(file);
			props.load(in);
			props.list(System.out);
		} finally {
			try {
				in.close();
			} catch (Exception e2) {
			}
		}
		return props;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		super.doPost(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		super.doGet(req, resp);
	}

}
